/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.portlet.NoSuchnguyenlieuException;

import com.portlet.model.impl.nguyenlieuImpl;
import com.portlet.model.impl.nguyenlieuModelImpl;
import com.portlet.model.nguyenlieu;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the nguyenlieu service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Thi
 * @see nguyenlieuPersistence
 * @see nguyenlieuUtil
 * @generated
 */
public class nguyenlieuPersistenceImpl extends BasePersistenceImpl<nguyenlieu>
	implements nguyenlieuPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link nguyenlieuUtil} to access the nguyenlieu persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = nguyenlieuImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuModelImpl.FINDER_CACHE_ENABLED, nguyenlieuImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuModelImpl.FINDER_CACHE_ENABLED, nguyenlieuImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the nguyenlieu in the entity cache if it is enabled.
	 *
	 * @param nguyenlieu the nguyenlieu
	 */
	public void cacheResult(nguyenlieu nguyenlieu) {
		EntityCacheUtil.putResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuImpl.class, nguyenlieu.getPrimaryKey(), nguyenlieu);

		nguyenlieu.resetOriginalValues();
	}

	/**
	 * Caches the nguyenlieus in the entity cache if it is enabled.
	 *
	 * @param nguyenlieus the nguyenlieus
	 */
	public void cacheResult(List<nguyenlieu> nguyenlieus) {
		for (nguyenlieu nguyenlieu : nguyenlieus) {
			if (EntityCacheUtil.getResult(
						nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
						nguyenlieuImpl.class, nguyenlieu.getPrimaryKey()) == null) {
				cacheResult(nguyenlieu);
			}
			else {
				nguyenlieu.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all nguyenlieus.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(nguyenlieuImpl.class.getName());
		}

		EntityCacheUtil.clearCache(nguyenlieuImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the nguyenlieu.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(nguyenlieu nguyenlieu) {
		EntityCacheUtil.removeResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuImpl.class, nguyenlieu.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@Override
	public void clearCache(List<nguyenlieu> nguyenlieus) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (nguyenlieu nguyenlieu : nguyenlieus) {
			EntityCacheUtil.removeResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
				nguyenlieuImpl.class, nguyenlieu.getPrimaryKey());
		}
	}

	/**
	 * Creates a new nguyenlieu with the primary key. Does not add the nguyenlieu to the database.
	 *
	 * @param maNguyenLieu the primary key for the new nguyenlieu
	 * @return the new nguyenlieu
	 */
	public nguyenlieu create(String maNguyenLieu) {
		nguyenlieu nguyenlieu = new nguyenlieuImpl();

		nguyenlieu.setNew(true);
		nguyenlieu.setPrimaryKey(maNguyenLieu);

		return nguyenlieu;
	}

	/**
	 * Removes the nguyenlieu with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param maNguyenLieu the primary key of the nguyenlieu
	 * @return the nguyenlieu that was removed
	 * @throws com.portlet.NoSuchnguyenlieuException if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public nguyenlieu remove(String maNguyenLieu)
		throws NoSuchnguyenlieuException, SystemException {
		return remove((Serializable)maNguyenLieu);
	}

	/**
	 * Removes the nguyenlieu with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the nguyenlieu
	 * @return the nguyenlieu that was removed
	 * @throws com.portlet.NoSuchnguyenlieuException if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public nguyenlieu remove(Serializable primaryKey)
		throws NoSuchnguyenlieuException, SystemException {
		Session session = null;

		try {
			session = openSession();

			nguyenlieu nguyenlieu = (nguyenlieu)session.get(nguyenlieuImpl.class,
					primaryKey);

			if (nguyenlieu == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchnguyenlieuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(nguyenlieu);
		}
		catch (NoSuchnguyenlieuException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected nguyenlieu removeImpl(nguyenlieu nguyenlieu)
		throws SystemException {
		nguyenlieu = toUnwrappedModel(nguyenlieu);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, nguyenlieu);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(nguyenlieu);

		return nguyenlieu;
	}

	@Override
	public nguyenlieu updateImpl(com.portlet.model.nguyenlieu nguyenlieu,
		boolean merge) throws SystemException {
		nguyenlieu = toUnwrappedModel(nguyenlieu);

		boolean isNew = nguyenlieu.isNew();

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, nguyenlieu, merge);

			nguyenlieu.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		EntityCacheUtil.putResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
			nguyenlieuImpl.class, nguyenlieu.getPrimaryKey(), nguyenlieu);

		return nguyenlieu;
	}

	protected nguyenlieu toUnwrappedModel(nguyenlieu nguyenlieu) {
		if (nguyenlieu instanceof nguyenlieuImpl) {
			return nguyenlieu;
		}

		nguyenlieuImpl nguyenlieuImpl = new nguyenlieuImpl();

		nguyenlieuImpl.setNew(nguyenlieu.isNew());
		nguyenlieuImpl.setPrimaryKey(nguyenlieu.getPrimaryKey());

		nguyenlieuImpl.setMaNguyenLieu(nguyenlieu.getMaNguyenLieu());
		nguyenlieuImpl.setTenNguyenLieu(nguyenlieu.getTenNguyenLieu());
		nguyenlieuImpl.setGhiChu(nguyenlieu.getGhiChu());

		return nguyenlieuImpl;
	}

	/**
	 * Returns the nguyenlieu with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the nguyenlieu
	 * @return the nguyenlieu
	 * @throws com.liferay.portal.NoSuchModelException if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public nguyenlieu findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey((String)primaryKey);
	}

	/**
	 * Returns the nguyenlieu with the primary key or throws a {@link com.portlet.NoSuchnguyenlieuException} if it could not be found.
	 *
	 * @param maNguyenLieu the primary key of the nguyenlieu
	 * @return the nguyenlieu
	 * @throws com.portlet.NoSuchnguyenlieuException if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public nguyenlieu findByPrimaryKey(String maNguyenLieu)
		throws NoSuchnguyenlieuException, SystemException {
		nguyenlieu nguyenlieu = fetchByPrimaryKey(maNguyenLieu);

		if (nguyenlieu == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + maNguyenLieu);
			}

			throw new NoSuchnguyenlieuException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				maNguyenLieu);
		}

		return nguyenlieu;
	}

	/**
	 * Returns the nguyenlieu with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the nguyenlieu
	 * @return the nguyenlieu, or <code>null</code> if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public nguyenlieu fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey((String)primaryKey);
	}

	/**
	 * Returns the nguyenlieu with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param maNguyenLieu the primary key of the nguyenlieu
	 * @return the nguyenlieu, or <code>null</code> if a nguyenlieu with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public nguyenlieu fetchByPrimaryKey(String maNguyenLieu)
		throws SystemException {
		nguyenlieu nguyenlieu = (nguyenlieu)EntityCacheUtil.getResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
				nguyenlieuImpl.class, maNguyenLieu);

		if (nguyenlieu == _nullnguyenlieu) {
			return null;
		}

		if (nguyenlieu == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				nguyenlieu = (nguyenlieu)session.get(nguyenlieuImpl.class,
						maNguyenLieu);
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (nguyenlieu != null) {
					cacheResult(nguyenlieu);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(nguyenlieuModelImpl.ENTITY_CACHE_ENABLED,
						nguyenlieuImpl.class, maNguyenLieu, _nullnguyenlieu);
				}

				closeSession(session);
			}
		}

		return nguyenlieu;
	}

	/**
	 * Returns all the nguyenlieus.
	 *
	 * @return the nguyenlieus
	 * @throws SystemException if a system exception occurred
	 */
	public List<nguyenlieu> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the nguyenlieus.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of nguyenlieus
	 * @param end the upper bound of the range of nguyenlieus (not inclusive)
	 * @return the range of nguyenlieus
	 * @throws SystemException if a system exception occurred
	 */
	public List<nguyenlieu> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the nguyenlieus.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of nguyenlieus
	 * @param end the upper bound of the range of nguyenlieus (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of nguyenlieus
	 * @throws SystemException if a system exception occurred
	 */
	public List<nguyenlieu> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<nguyenlieu> list = (List<nguyenlieu>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_NGUYENLIEU);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_NGUYENLIEU;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<nguyenlieu>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<nguyenlieu>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the nguyenlieus from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (nguyenlieu nguyenlieu : findAll()) {
			remove(nguyenlieu);
		}
	}

	/**
	 * Returns the number of nguyenlieus.
	 *
	 * @return the number of nguyenlieus
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_NGUYENLIEU);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns all the nlhassps associated with the nguyenlieu.
	 *
	 * @param pk the primary key of the nguyenlieu
	 * @return the nlhassps associated with the nguyenlieu
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nlhassp> getnlhassps(String pk)
		throws SystemException {
		return getnlhassps(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	}

	/**
	 * Returns a range of all the nlhassps associated with the nguyenlieu.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the nguyenlieu
	 * @param start the lower bound of the range of nguyenlieus
	 * @param end the upper bound of the range of nguyenlieus (not inclusive)
	 * @return the range of nlhassps associated with the nguyenlieu
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nlhassp> getnlhassps(String pk, int start,
		int end) throws SystemException {
		return getnlhassps(pk, start, end, null);
	}

	public static final FinderPath FINDER_PATH_GET_NLHASSPS = new FinderPath(com.portlet.model.impl.nlhasspModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspImpl.class,
			com.portlet.service.persistence.nlhasspPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getnlhassps",
			new String[] {
				String.class.getName(), "java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});

	static {
		FINDER_PATH_GET_NLHASSPS.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns an ordered range of all the nlhassps associated with the nguyenlieu.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the nguyenlieu
	 * @param start the lower bound of the range of nguyenlieus
	 * @param end the upper bound of the range of nguyenlieus (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of nlhassps associated with the nguyenlieu
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.portlet.model.nlhassp> getnlhassps(String pk, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };

		List<com.portlet.model.nlhassp> list = (List<com.portlet.model.nlhassp>)FinderCacheUtil.getResult(FINDER_PATH_GET_NLHASSPS,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				String sql = null;

				if (orderByComparator != null) {
					sql = _SQL_GETNLHASSPS.concat(ORDER_BY_CLAUSE)
										  .concat(orderByComparator.getOrderBy());
				}
				else {
					sql = _SQL_GETNLHASSPS;
				}

				SQLQuery q = session.createSQLQuery(sql);

				q.addEntity("congty_nlhassp",
					com.portlet.model.impl.nlhasspImpl.class);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				list = (List<com.portlet.model.nlhassp>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_GET_NLHASSPS,
						finderArgs);
				}
				else {
					nlhasspPersistence.cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_GET_NLHASSPS,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	public static final FinderPath FINDER_PATH_GET_NLHASSPS_SIZE = new FinderPath(com.portlet.model.impl.nlhasspModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspImpl.class,
			com.portlet.service.persistence.nlhasspPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getnlhasspsSize", new String[] { String.class.getName() });

	static {
		FINDER_PATH_GET_NLHASSPS_SIZE.setCacheKeyGeneratorCacheName(null);
	}

	/**
	 * Returns the number of nlhassps associated with the nguyenlieu.
	 *
	 * @param pk the primary key of the nguyenlieu
	 * @return the number of nlhassps associated with the nguyenlieu
	 * @throws SystemException if a system exception occurred
	 */
	public int getnlhasspsSize(String pk) throws SystemException {
		Object[] finderArgs = new Object[] { pk };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_NLHASSPS_SIZE,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				SQLQuery q = session.createSQLQuery(_SQL_GETNLHASSPSSIZE);

				q.addScalar(COUNT_COLUMN_NAME,
					com.liferay.portal.kernel.dao.orm.Type.LONG);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_GET_NLHASSPS_SIZE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	public static final FinderPath FINDER_PATH_CONTAINS_NLHASSP = new FinderPath(com.portlet.model.impl.nlhasspModelImpl.ENTITY_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspModelImpl.FINDER_CACHE_ENABLED,
			com.portlet.model.impl.nlhasspImpl.class,
			com.portlet.service.persistence.nlhasspPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"containsnlhassp",
			new String[] { String.class.getName(), Long.class.getName() });

	/**
	 * Returns <code>true</code> if the nlhassp is associated with the nguyenlieu.
	 *
	 * @param pk the primary key of the nguyenlieu
	 * @param nlhasspPK the primary key of the nlhassp
	 * @return <code>true</code> if the nlhassp is associated with the nguyenlieu; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsnlhassp(String pk, long nlhasspPK)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, nlhasspPK };

		Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_NLHASSP,
				finderArgs, this);

		if (value == null) {
			try {
				value = Boolean.valueOf(containsnlhassp.contains(pk, nlhasspPK));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (value == null) {
					value = Boolean.FALSE;
				}

				FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_NLHASSP,
					finderArgs, value);
			}
		}

		return value.booleanValue();
	}

	/**
	 * Returns <code>true</code> if the nguyenlieu has any nlhassps associated with it.
	 *
	 * @param pk the primary key of the nguyenlieu to check for associations with nlhassps
	 * @return <code>true</code> if the nguyenlieu has any nlhassps associated with it; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsnlhassps(String pk) throws SystemException {
		if (getnlhasspsSize(pk) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Initializes the nguyenlieu persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.portlet.model.nguyenlieu")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<nguyenlieu>> listenersList = new ArrayList<ModelListener<nguyenlieu>>();

				for (String listenerClassName : listenerClassNames) {
					Class<?> clazz = getClass();

					listenersList.add((ModelListener<nguyenlieu>)InstanceFactory.newInstance(
							clazz.getClassLoader(), listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}

		containsnlhassp = new Containsnlhassp();
	}

	public void destroy() {
		EntityCacheUtil.removeCache(nguyenlieuImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = nguyenlieuPersistence.class)
	protected nguyenlieuPersistence nguyenlieuPersistence;
	@BeanReference(type = nlhasspPersistence.class)
	protected nlhasspPersistence nlhasspPersistence;
	@BeanReference(type = quycachSPPersistence.class)
	protected quycachSPPersistence quycachSPPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	protected Containsnlhassp containsnlhassp;

	protected class Containsnlhassp {
		protected Containsnlhassp() {
			_mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
					_SQL_CONTAINSNLHASSP,
					new int[] { java.sql.Types.VARCHAR, java.sql.Types.BIGINT },
					RowMapper.COUNT);
		}

		protected boolean contains(String maNguyenLieu, long maNLSP) {
			List<Integer> results = _mappingSqlQuery.execute(new Object[] {
						maNguyenLieu, new Long(maNLSP)
					});

			if (results.size() > 0) {
				Integer count = results.get(0);

				if (count.intValue() > 0) {
					return true;
				}
			}

			return false;
		}

		private MappingSqlQuery<Integer> _mappingSqlQuery;
	}

	private static final String _SQL_SELECT_NGUYENLIEU = "SELECT nguyenlieu FROM nguyenlieu nguyenlieu";
	private static final String _SQL_COUNT_NGUYENLIEU = "SELECT COUNT(nguyenlieu) FROM nguyenlieu nguyenlieu";
	private static final String _SQL_GETNLHASSPS = "SELECT {congty_nlhassp.*} FROM congty_nlhassp INNER JOIN congty_nguyenlieu ON (congty_nguyenlieu.maNguyenLieu = congty_nlhassp.maNguyenLieu) WHERE (congty_nguyenlieu.maNguyenLieu = ?)";
	private static final String _SQL_GETNLHASSPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM congty_nlhassp WHERE maNguyenLieu = ?";
	private static final String _SQL_CONTAINSNLHASSP = "SELECT COUNT(*) AS COUNT_VALUE FROM congty_nlhassp WHERE maNguyenLieu = ? AND maNLSP = ?";
	private static final String _ORDER_BY_ENTITY_ALIAS = "nguyenlieu.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No nguyenlieu exists with the primary key ";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(nguyenlieuPersistenceImpl.class);
	private static nguyenlieu _nullnguyenlieu = new nguyenlieuImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<nguyenlieu> toCacheModel() {
				return _nullnguyenlieuCacheModel;
			}
		};

	private static CacheModel<nguyenlieu> _nullnguyenlieuCacheModel = new CacheModel<nguyenlieu>() {
			public nguyenlieu toEntityModel() {
				return _nullnguyenlieu;
			}
		};
}